<?php
include_once('parameters.php');

generateMapping();

class TableStructure{
	public $name;
	public $primaryKeyFieldName;
	public $fields=array();
	public $foreignKeys=array(); //array key is the fieldName, array value is the referenced table name
	public $childKeys=array(); //array of ChildkeyStructure
}

class ChildkeyStructure{
	public $tableName;
	public $columnName;
	public $accessor;
}

function generateMapping(){
	try {
		//Get all the schema tables
		$l_dbh = new PDO(__LOCAL_GEN_DB_URL, __LOCAL_GEN_DB_USER, __LOCAL_GEN_DB_PASSWORD);
		foreach ($l_dbh->query("SHOW TABLES") as $l_row) {
			generateBo($l_row[0]);
		}
		$l_dbh = null;
	} catch (PDOException $e) {
		print "Error ! : " . $e->getMessage() . "<br/>";
		die();
	}	
}

function generateBo($p_tableName){
	$l_className=ucfirst($p_tableName);
	$l_tableStructure=getTableStructure($p_tableName);

	//Generate base class (if the base class already exists, it is not overwritten)
	ob_start();
	include "boClassTemplate.php";
    $l_contents = ob_get_contents();
    $l_contents="<?php\n{$l_contents}\n?>";
    ob_end_clean();
    $l_generatedFileName=__LOCAL_GEN_DB_OUTPUT;
    $l_generatedFileName="{$l_generatedFileName}/{$l_className}.php";
    if(!file_exists($l_generatedFileName)){
    	file_put_contents($l_generatedFileName,$l_contents);
    }
    
	//Generate DAO class
	ob_start();
	include "boDaoTemplate.php";
    $l_contents = ob_get_contents();
    $l_contents="<?php\n{$l_contents}\n?>";
    ob_end_clean();
    $l_generatedFileName=__LOCAL_GEN_DB_OUTPUT;
    $l_generatedFileName="{$l_generatedFileName}/{$l_className}_DAO.php";
    file_put_contents($l_generatedFileName,$l_contents);
    
    echo("<b>Generated business object:</b> {$l_className}<br>");
}

/**
 * Return a description of the table (object TableStructure)
 * @param string $p_tableName the name of a DB table 
 * @return TableStructure
 */
function getTableStructure($p_tableName){
	$l_tableStructure=new TableStructure();
	$l_tableStructure->name=$p_tableName;
	try {
		//Get the table fields
		$l_dbh = new PDO(__LOCAL_GEN_DB_URL, __LOCAL_GEN_DB_USER, __LOCAL_GEN_DB_PASSWORD);
		foreach ($l_dbh->query("SHOW COLUMNS FROM {$p_tableName}") as $l_row) {
			$l_tableStructure->fields[$l_row['Field']]=$l_row['Type'];
		}
		
		//Look for foreign keys
		foreach ($l_dbh->query("select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where TABLE_NAME='{$p_tableName}' and TABLE_SCHEMA='" . __LOCAL_DB_SCHEMA . "'") as $l_row) {
			if($l_row['CONSTRAINT_NAME']=="PRIMARY"){
				$l_tableStructure->primaryKeyFieldName=$l_row['COLUMN_NAME'];
			}else{
				$l_tableStructure->foreignKeys[$l_row['COLUMN_NAME']]=$l_row['REFERENCED_TABLE_NAME'];
			}
		}
		
		//Look for child keys
		$tableCount=array();
		foreach ($l_dbh->query("select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME='{$p_tableName}' and TABLE_SCHEMA='" . __LOCAL_DB_SCHEMA . "'") as $l_row) {
			if(isset($tableCount[$l_row['TABLE_NAME']])){
				$tableCount[$l_row['TABLE_NAME']]=$tableCount[$l_row['TABLE_NAME']]+1;
			}else{
				$tableCount[$l_row['TABLE_NAME']]=0;
			}
		}	
		foreach ($l_dbh->query("select * from INFORMATION_SCHEMA.KEY_COLUMN_USAGE where REFERENCED_TABLE_NAME='{$p_tableName}' and TABLE_SCHEMA='" . __LOCAL_DB_SCHEMA . "'") as $l_row) {
			$l_childKey=new ChildkeyStructure();
			$l_childKey->tableName=$l_row['TABLE_NAME'];
			$l_childKey->columnName=$l_row['COLUMN_NAME'];
			if($tableCount[$l_row['TABLE_NAME']]==0){
				$l_childKey->accessor=$l_row['TABLE_NAME'];
			}else{
				$l_extension=substr($l_row['CONSTRAINT_NAME'], strrpos($l_row['CONSTRAINT_NAME'],"_")+1);
				$l_childKey->accessor=$l_extension . "_" . $l_row['TABLE_NAME'];
			}
			$l_tableStructure->childKeys[]=$l_childKey;
		}
		
		$l_dbh = null;
	} catch (PDOException $e) {
		print "Error ! : " . $e->getMessage() . "<br/>";
		die();
	}
	return $l_tableStructure;
}
?>